﻿using CsvHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using TweetSharp;

namespace Microsoft.AzureAD.XPrize.TwittOracle
{
    class Utils
    {
        public static string GetScreenName(string url)
        {
            Regex regex = new Regex(@"https?://twitter.com/(#!/)?([^/]+)(/.*)?$");
            MatchCollection m = regex.Matches(url);
            if(m.Count != 1 || m[0].Groups.Count != 4)
                return null;
            else
                return m[0].Groups[2].Value;
        }

        public static void CsvWrite(CsvWriter csvWriter, TwitterUser user)
        {
            csvWriter.WriteField(user.ScreenName);
            csvWriter.WriteField(user.Name, true);
            csvWriter.WriteField(user.FavouritesCount);
            csvWriter.WriteField(user.FollowersCount);
            csvWriter.WriteField(user.FriendsCount);
            csvWriter.WriteField(user.ListedCount);
            csvWriter.WriteField(user.StatusesCount);
            csvWriter.WriteField(user.IsVerified);

            csvWriter.WriteField(user.Id);
            csvWriter.WriteField(user.ContributorsEnabled);
            csvWriter.WriteField(user.CreatedDate);
            csvWriter.WriteField(String.IsNullOrEmpty(user.Description).ToString());
            csvWriter.WriteField(user.FollowRequestSent);
            csvWriter.WriteField(user.IsDefaultProfile);
            csvWriter.WriteField(user.IsGeoEnabled);
            csvWriter.WriteField(user.IsProfileBackgroundTiled);
            csvWriter.WriteField(user.IsProtected);
            csvWriter.WriteField(user.IsTranslator);
            csvWriter.WriteField(user.Language);
            csvWriter.WriteField(user.Location, true);
            csvWriter.WriteField(user.ProfileBackgroundColor);
            csvWriter.WriteField(user.ProfileBackgroundImageUrl);
            csvWriter.WriteField(user.ProfileBackgroundImageUrlHttps);
            csvWriter.WriteField(user.ProfileImageUrl);
            csvWriter.WriteField(user.ProfileImageUrlHttps);
            csvWriter.WriteField(user.ProfileLinkColor);
            csvWriter.WriteField(user.ProfileSidebarBorderColor);
            csvWriter.WriteField(user.ProfileSidebarFillColor);
            csvWriter.WriteField(user.ProfileTextColor);
            csvWriter.WriteField((user.Status != null).ToString());
            csvWriter.WriteField(user.TimeZone);
            csvWriter.WriteField(user.Url);
            csvWriter.WriteField(user.UtcOffset);

            csvWriter.NextRecord();
        }
    }
}
